package cn.iocoder.yudao.module.fm.controller.admin.bridge;

import cn.iocoder.yudao.module.fm.enums.FileInfoType;
import cn.iocoder.yudao.module.fm.service.fileinfo.FileInfoService;
import cn.iocoder.yudao.module.fm.utils.DownloadTemplateUtil;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.operatelog.core.annotations.OperateLog;
import static cn.iocoder.yudao.framework.operatelog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.fm.controller.admin.bridge.vo.*;
import cn.iocoder.yudao.module.fm.dal.dataobject.bridge.BridgeDO;
import cn.iocoder.yudao.module.fm.convert.bridge.BridgeConvert;
import cn.iocoder.yudao.module.fm.service.bridge.BridgeService;
import org.springframework.web.multipart.MultipartFile;

@Tag(name = "管理后台 - 桥梁")
@RestController
@RequestMapping("/fm/bridge")
@Validated
public class BridgeController {

    @Resource
    private BridgeService bridgeService;
    @Resource
    private FileInfoService fileInfoService;


    @PostMapping("/create")
    @Operation(summary = "创建桥梁")
    @PreAuthorize("@ss.hasPermission('fm:bridge:create')")
    public CommonResult<Long> createSubgrade(@Valid @RequestBody BridgeCreateReqVO createReqVO) {
        return success(bridgeService.createBridge(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新桥梁")
    @PreAuthorize("@ss.hasPermission('fm:bridge:update')")
    public CommonResult<Boolean> updateSubgrade(@Valid @RequestBody BridgeUpdateReqVO updateReqVO) {
        bridgeService.updateBridge(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除桥梁")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('fm:bridge:delete')")
    public CommonResult<Boolean> deleteSubgrade(@RequestParam("id") Long id) {
        bridgeService.deleteBridge(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得桥梁")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('fm:bridge:query')")
    public CommonResult<BridgeRespVO> getBridge(@RequestParam("id") Long id) {
        BridgeDO bridge = bridgeService.getBridge(id);
        BridgeRespVO bridgeRespVO = BridgeConvert.INSTANCE.convert(bridge);
        bridgeRespVO.setFileList(fileInfoService.get(bridgeRespVO.getId(), FileInfoType.BRIDGE.getValue()));
        return success(bridgeRespVO);
    }

    @GetMapping("/list")
    @Operation(summary = "获得桥梁列表")
    @Parameter(name = "ids", description = "编号列表", required = true, example = "1024,2048")
    @PreAuthorize("@ss.hasPermission('fm:bridge:query')")
    public CommonResult<List<BridgeRespVO>> getSubgradeList(@RequestParam("ids") Collection<Long> ids) {
        List<BridgeDO> list = bridgeService.getBridgeList(ids);
        return success(BridgeConvert.INSTANCE.convertList(list));
    }

    @GetMapping("/page")
    @Operation(summary = "获得桥梁分页")
    @PreAuthorize("@ss.hasPermission('fm:bridge:query')")
    public CommonResult<PageResult<BridgeRespVO>> getSubgradePage(@Valid BridgePageReqVO pageVO) {
        PageResult<BridgeDO> pageResult = bridgeService.getBridgePage(pageVO);
        return success(BridgeConvert.INSTANCE.convertPage(pageResult));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出桥梁 Excel")
    @PreAuthorize("@ss.hasPermission('fm:bridge:export')")
    @OperateLog(type = EXPORT)
    public void exportSubgradeExcel(@Valid BridgeExportReqVO exportReqVO,
              HttpServletResponse response) throws IOException {
        List<BridgeDO> list = bridgeService.getBridgeList(exportReqVO);
        // 导出 Excel
        List<BridgeExcelVO> datas = BridgeConvert.INSTANCE.convertList02(list);
        ExcelUtils.write(response, "桥梁.xls", "数据", BridgeExcelVO.class, datas);
    }


    @GetMapping("/get-import-template")
    @Operation(summary = "获得导入模板")
    public ResponseEntity<byte[]> importTemplate() {
        return DownloadTemplateUtil.downloadTemplate("bridgeTemplate.xlsx", "template/bridge-export-template-download.xlsx");
    }

    @PostMapping("/import")
    @Operation(summary = "导入")
    @PreAuthorize("@ss.hasPermission('system:user:import')")
    public CommonResult<Boolean> importExcel(@RequestParam("file") MultipartFile file) throws Exception {
        bridgeService.importExcel(file);
        return CommonResult.success(true);
    }

    @GetMapping("/export")
    @Operation(summary = "导出")
    @PreAuthorize("@ss.hasPermission('system:tunnel:export')")
    public ResponseEntity<byte[]> exportExcel(Long id) {
        return bridgeService.exportExcel(getBridge(id).getData());
    }


}
